package org.snusmumr.android.media.data;

import java.util.ArrayList;
import java.util.Date;

import org.snusmumr.android.media.Constants;
import org.snusmumr.android.media.data.DatabaseProvider.DatabaseMode;
import org.snusmumr.android.media.util.Utils;

import android.content.ContentValues;
import android.content.Context;
import android.widget.Toast;

public class LentItemsOperation extends DBOperation<Boolean> {
	private String contactId;
	private String contactName;
	private ArrayList<Long> selectedItems;

	public LentItemsOperation(Context context, String contactId, String contactName, ArrayList<Long> selectedItems) {
		super(context);
		this.contactId = contactId;
		this.contactName = contactName;
		this.selectedItems = selectedItems;
	}
	
	@Override
	protected DatabaseMode getDatabaseMode() {
		return DatabaseMode.Write;
	}

	@Override
	public Boolean execute() {
		if(selectedItems == null || selectedItems.size() == 0) {
			Toast.makeText(getContext(), "_No items selected", Toast.LENGTH_SHORT).show();
			return Boolean.FALSE;
		} else {
			Long friendId = getFrendId(contactId, contactName);
			ContentValues values = new ContentValues();
			values.put(MediaColumns.LEND_DATE, Utils.getDateAsStringForDB(new Date()));
			values.put(MediaColumns.LEND_TO, friendId);
			String whereClause = MediaColumns._ID + 
			" in(" + Utils.getAsString(selectedItems) + ')';
			getDatabaseProvider().getDatabase(getDatabaseMode()).update(Constants.DB_TABLE_MEDIA_ITEM, 
						values, 
						whereClause, 
						null);
			getDatabaseProvider().close();
			return Boolean.TRUE;
		}
	}

}
